﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RainyPWS.Common
{
    public class MyQueryParam
    {
        #region "Private Variables"
        private string _TableName;
        private string _ReturnFields;
        private string _Where;
        private string _Orderfld;
        private int _PageIndex = 1;
        private int _PageSize = int.MaxValue;
        private string _TableJoin;
        private string _Keyfld;
        #endregion

        #region "Public Variables"

        /// <summary>
        /// 表名
        /// </summary>
        public string TableName
        {
            get
            {
                return _TableName;
            }
            set
            {
                _TableName = value;
            }

        }

        /// <summary>
        /// 返回字段
        /// </summary>
        public string ReturnFields
        {
            get
            {
                return _ReturnFields;
            }
            set
            {
                _ReturnFields = value;
            }
        }

        /// <summary>
        /// 查询条件 需带Where
        /// </summary>
        public string Where
        {
            get
            {
                return _Where;
            }
            set
            {
                _Where = value;
            }
        }

        /// <summary>
        /// 排序字段
        /// </summary>
        public string Orderfld
        {
            get
            {
                return _Orderfld;
            }
            set
            {
                _Orderfld = value;
            }
        }

        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageIndex
        {
            get
            {
                return _PageIndex;
            }
            set
            {
                _PageIndex = value;
            }

        }

        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize
        {
            get
            {
                return _PageSize;
            }
            set
            {
                _PageSize = value;
            }
        }

        /// <summary>
        /// 多表链接
        /// </summary>
        public string TableJoin
        {
            get
            {
                return _TableJoin;
            }
            set
            {
                _TableJoin = value;
            }
        }
        /// <summary>
        /// 关键字段
        /// </summary>
        public string Keyfld
        {
            get
            {
                return _Keyfld;
            }
            set
            {
                _Keyfld = value;
            }
        }


        #endregion

        #region "Public Function"
        /// <summary>
        /// 返回分页查询sql
        /// </summary>
        /// <returns></returns>
        public string GetSql()
        {
            if (Where == "")
                Where = "where 1=1";
            if (Keyfld == "")
                return "";
            string tempsql, sql;
            if (PageIndex > 1)
            {
                tempsql = string.Format("select top {0} {1} from {2} {3} {4} Order by {5}", (PageIndex - 1) * PageSize, Keyfld, TableName, TableJoin, Where, Orderfld);
                sql = string.Format("select top {0} {1} from {2} {3} {4} and {5} not in ({6}) Order by {7}", PageSize, ReturnFields, TableName, TableJoin, Where, Keyfld, tempsql, Orderfld);
            }
            else
            {
                sql = string.Format("select top {0} {1} from {2} {3} {4} Order by {5}", PageSize, ReturnFields, TableName, TableJoin, Where, Orderfld);
            }
            return sql;
        }
        /// <summary>
        /// 返回数据总计录数
        /// </summary>
        /// <returns></returns>
        public string GetSqlCount()
        {
            string sqlcount = string.Format("select count(1) from {0} {1} {2}", TableName, TableJoin, Where);
            return sqlcount;
        }
        #endregion
    }
}
